/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @ShareCookie.java  2014-9-9 上午10:55:51 - Carson
 * @author YanXu
 * @email:981385016@qq.com
 * @version 1.0
 */

package com.android.tiango.baba.cookie;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.android.tiango.baba.BaBaseApplication;
import com.android.tiango.baba.entity.UserModel;
import com.android.tiango.baba.util.Constants;

public class ShareCookie {

	private final static String USER_INFO = "user_infos";

	public static boolean isLoginAuth() {
		SharedPreferences cookie = BaBaseApplication.getAppContext()
				.getSharedPreferences(Constants.COOKIE_FILE,
						Context.MODE_PRIVATE);
		return cookie.getBoolean(Constants.IS_LOGIN_AUTH, false);
	}

	public static void setLoginAuth(boolean login) {
		SharedPreferences cookie = BaBaseApplication.getAppContext()
				.getSharedPreferences(Constants.COOKIE_FILE,
						Context.MODE_PRIVATE);
		Editor editor = cookie.edit();
		editor.putBoolean(Constants.IS_LOGIN_AUTH, login);
		editor.commit();
	}

	public static long getCCTime() {
		SharedPreferences cookie = BaBaseApplication.getAppContext()
				.getSharedPreferences(Constants.COOKIE_FILE,
						Context.MODE_PRIVATE);
		return cookie.getLong(Constants.CCTIME, 0);
	}

	public static void saveCCTime(long time) {
		SharedPreferences cookie = BaBaseApplication.getAppContext()
				.getSharedPreferences(Constants.COOKIE_FILE,
						Context.MODE_PRIVATE);
		Editor editor = cookie.edit();
		editor.putLong(Constants.CCTIME, time);
		editor.commit();
	}

	public static String getToken() {
		UserModel bean = getUserInfo();
		if (null != bean) {
			return bean.getToken();
		} else {
			return "";
		}
	}

	public static boolean saveUserInfo(UserModel model) {
		boolean ret = false;
		if (null == model) {
			return false;
		}
		FileOutputStream outStream = null;
		try {
			outStream = BaBaseApplication.getAppContext().openFileOutput(
					USER_INFO, Context.MODE_PRIVATE);
			ObjectOutputStream objectStream = new ObjectOutputStream(outStream);
			objectStream.writeObject(model);
			ret = true;
		} catch (IOException e) {
			ret = false;
		}
		if (null != outStream) {
			try {
				outStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}

	public static UserModel getUserInfo() {
		UserModel entity = null;
		FileInputStream fin = null;
		try {
			fin = BaBaseApplication.getAppContext().openFileInput(USER_INFO);
			ObjectInputStream inStream = new ObjectInputStream(fin);
			entity = (UserModel) inStream.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (null != fin) {
			try {
				fin.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (entity == null) {
			ShareCookie.setLoginAuth(false);
		}
		return entity;

	}

	public static void clearUserInfo() {
		BaBaseApplication.getAppContext().deleteFile(USER_INFO);
		setLoginAuth(false);
	}

	public static String getUserAccount() {
		UserModel user = getUserInfo();
		if (user != null) {
			return user.getNickName();
		}
		return "";
	}
}
